#include <Ape/Collections/StringList.h>
#include <Ape/IO/Console.h>

namespace Ape {
	namespace Collections {
		StringList::StringList() {
		
		}
		StringList::~StringList() {
		
		}
		
		String StringList::Join (const String& glue) const {
		
#define APE_COPY_DATA_TO_BUFFER(dest, src, len) \
	for (int i = 0; i < len; i++) {\
		dest[0] = src.WcharAt(i); \
		dest++; \
	}
			int iGlueSize = glue.get_Length();
			int iResultSize = 0;
			
			foreach (part, StringList, *this) {
				iResultSize += ( (String) part).get_Length();
			}
			iResultSize += iGlueSize * (get_Count() - 1);
			
			wchar_t* buff = new wchar_t[iResultSize + 2];
			
			wchar_t* cursor = buff;
			
			foreach (part, StringList, *this) {
				int len = ( (String) part).get_Length();
				APE_COPY_DATA_TO_BUFFER (cursor, ( (String) part), len);
				
				if (part.Continue() ) {
					len = glue.get_Length();
					APE_COPY_DATA_TO_BUFFER (cursor, glue, len);
				}
			}
			buff[iResultSize] = L'\0';
			
			String result (buff);
			del_a (buff);
			
			return result;
#undef APE_COPY_DATA_TO_BUFFER
		}
	}
}
